package remote.layouts
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	import remote.collections.IList;
	import remote.controls.Component;
	import remote.geom.EdgeMetrics;
	import remote.utils.DisplayObjectUtil;
	
	
	/**
	 * 有边距的布局，和绝对布局不同的是，此布局会将范围之外的对象移到范围内
	 * @author 银魂飞雪
	 * @createDate 2011-2-27
	 */
	public class PaddingLayout extends LayoutBase implements ILayout
	{
		private var _paddingLeft:Number;
		private var _paddingTop:Number;
		private var _paddingRight:Number;
		private var _paddingBottom:Number;

		
		/**
		 * 实例化边距布局 
		 * @param target 布局应用的对象
		 * @param paddingLeft 左边距
		 * @param paddingTop 上边距
		 * @param paddingRight 右边距
		 * @param paddingBottom 下边距
		 * 
		 */		
		public function PaddingLayout(target:Component = null, paddingLeft:Number = 0, paddingTop:Number = 0, paddingRight:Number = 0, paddingBottom:Number = 0)
		{
			super(target);
			_paddingLeft = paddingLeft;
			_paddingTop = paddingTop;
			_paddingRight = paddingRight;
			_paddingBottom = paddingBottom;
		}
		
		public function layout():void
		{
			for each(var child:DisplayObject in elements)
			{
				if(child.x < _paddingLeft)
					child.x = _paddingLeft;
				if(child.x > target.width - _paddingRight)
					child.x = target.width - _paddingRight;
				if(child.y < _paddingTop)
					child.y = _paddingTop;
				if(child.y > target.height - _paddingBottom)
					child.y = target.height - _paddingBottom;
			}
		}
		

		public function get edge():EdgeMetrics
		{
			return new EdgeMetrics(_paddingLeft, _paddingTop, _paddingRight, _paddingBottom);
		}
		
		public function get contentRect():Rectangle
		{
			return new Rectangle(_paddingLeft, _paddingTop, target.width - _paddingLeft - _paddingRight, target.height - _paddingTop - _paddingBottom);
		}
		
		public function get childrenRect():Rectangle
		{
			if(elements.length == 0)
				return new Rectangle();
			var result:Rectangle = new Rectangle(elements[0].x, elements[0].y, elements[0].width, elements[0].height);
			for(var i:int = 1; i < elements.length; i++)
			{
				var child:DisplayObject = elements[i];
				if(child.x < result.left)
					result.left = child.x;
				if(child.x + child.width > result.right)
					result.right = child.x + child.width;
				if(child.y < result.top)
					result.top = child.y;
				if(child.y + child.height > result.bottom)
					result.bottom = child.y + child.height;
			}
			return result;
		}
		
		/**
		 * 左边距
		 * 
		 */		
		public function get paddingLeft():Number
		{
			return _paddingLeft;
		}

		public function set paddingLeft(value:Number):void
		{
			_paddingLeft = value;
		}

		/**
		 * 上边距
		 * 
		 */	
		public function get paddingTop():Number
		{
			return _paddingTop;
		}

		public function set paddingTop(value:Number):void
		{
			_paddingTop = value;
		}

		/**
		 * 右边距
		 * 
		 */	
		public function get paddingRight():Number
		{
			return _paddingRight;
		}

		public function set paddingRight(value:Number):void
		{
			_paddingRight = value;
		}

		/**
		 * 下边距
		 * 
		 */	
		public function get paddingBottom():Number
		{
			return _paddingBottom;
		}

		public function set paddingBottom(value:Number):void
		{
			_paddingBottom = value;
		}

		/**
		 * 设置所有边距为相同的值
		 * 
		 */		
		public function set padding(value:Number):void
		{
			_paddingLeft = value;
			_paddingTop = value;
			_paddingRight = value;
			_paddingBottom = value;
			
		}

	}
}